#include "./../inc/main.h"

// 创建角色
per_p perInit()
{
    per_p temp = (per_p)malloc(sizeof(per_t));
    memset(temp, 0, sizeof(per_t));
    strcpy(temp->name, "player");
    temp->HP = 100;
    temp->ATK = 2;
    temp->DEF = 1;
    temp->gold = 0;
    temp->map = 0;
    temp->loc[0] = 1;
    temp->loc[1] = 1;
    return temp;
}

// 角色移动函数
int move(per_p per)
{
    int getkey = get_arrow_key();
    int map_info = 0;
    int prev_loc[2] = {per->loc[0], per->loc[1]};
    if ((getkey == UP || getkey + 32 == 'W') && curr_map->map[per->loc[0] - 1][per->loc[1]] != WALL)
    {
        curr_map->map[per->loc[0]][per->loc[1]] = 0;
        map_info = curr_map->map[per->loc[0] - 1][per->loc[1]];
        curr_map->map[per->loc[0] - 1][per->loc[1]] = PLAYER;
        per->loc[0] = per->loc[0] - 1;
        if (event(per, map_info) == 1)
        {
            curr_map->map[per->loc[0]][per->loc[1]] = map_info;
            per->loc[0] = per->loc[0] + 1;
            curr_map->map[per->loc[0]][per->loc[1]] = PLAYER;
        }
    }
    else if ((getkey == DOWN || getkey + 32 == 'S') && curr_map->map[per->loc[0] + 1][per->loc[1]] != WALL)
    {
        curr_map->map[per->loc[0]][per->loc[1]] = 0;
        map_info = curr_map->map[per->loc[0] + 1][per->loc[1]];
        curr_map->map[per->loc[0] + 1][per->loc[1]] = PLAYER;
        per->loc[0] = per->loc[0] + 1;

        if (event(per, map_info) == 1)
        {
            curr_map->map[per->loc[0]][per->loc[1]] = map_info;
            per->loc[0] = per->loc[0] - 1;
            curr_map->map[per->loc[0]][per->loc[1]] = PLAYER;
        }
    }
    else if ((getkey == LEFT || getkey + 32 == 'A') && curr_map->map[per->loc[0]][per->loc[1] - 1] != WALL)
    {
        curr_map->map[per->loc[0]][per->loc[1]] = 0;
        map_info = curr_map->map[per->loc[0]][per->loc[1] - 1];
        curr_map->map[per->loc[0]][per->loc[1] - 1] = PLAYER;
        per->loc[1] = per->loc[1] - 1;

        if (event(per, map_info) == 1)
        {
            curr_map->map[per->loc[0]][per->loc[1]] = map_info;
            per->loc[1] = per->loc[1] + 1;
            curr_map->map[per->loc[0]][per->loc[1]] = PLAYER;
        }
    }
    else if ((getkey == RIGHT || getkey + 32 == 'D') && curr_map->map[per->loc[0]][per->loc[1] + 1] != WALL)
    {
        curr_map->map[per->loc[0]][per->loc[1]] = 0;
        map_info = curr_map->map[per->loc[0]][per->loc[1] + 1];
        curr_map->map[per->loc[0]][per->loc[1] + 1] = PLAYER;
        per->loc[1] = per->loc[1] + 1;

        if (event(per, map_info) == 1)
        {
            curr_map->map[per->loc[0]][per->loc[1]] = map_info;
            per->loc[1] = per->loc[1] - 1;
            curr_map->map[per->loc[0]][per->loc[1]] = PLAYER;
        }
    }
    else if ((getkey == SAVE || getkey + 32 == 'Z'))
    {
        save();
    }
        if (getkey == ESC)
            return 0;

    system("cls");
    chess(curr_map);
    return 1;
}

// index 0 成功 1 失败
int event(per_p per, int map_info)
{
    int index = 0;
    if (map_info == PORTAL_UP)
    {
        curr_map->map[per->loc[0]][per->loc[1]] = map_info; // 保存传送门
        save_map();
        map_index--;
        curr_map = mapInit();
        per->loc[0] = 13;
        per->loc[1] = 18;
        curr_map->map[per->loc[0]][per->loc[1]] = PLAYER;
        per->map = map_index;
        save_per();
    }
    else if (map_info == PORTAL_DOWN)
    {
        curr_map->map[per->loc[0]][per->loc[1]] = map_info; // 保存传送门
        save_map();
        map_index++;
        curr_map = mapInit();
        per->loc[0] = 1;
        per->loc[1] = 1;
        curr_map->map[per->loc[0]][per->loc[1]] = PLAYER;
        per->map = map_index;
        save_per();
    }
    else if (map_info >= SLIME)
    {
        monster_Node_p currNode = find_monster(map_info);
        int status = fight(per, currNode);
        if (status == 1)
        {
            index = 1;
        }
    }
    return index;
}